from django.http import HttpResponse, HttpResponseRedirect
from jieblog.applications import models
from jieblog.applications import bforms
from django.shortcuts import render_to_response


def render(template, payload):
	return render_to_response(template, payload)

def index(request):
	posts = models.Post.all().fetch(20)
	payload = dict(posts = posts)
	return render('index.html', payload)
	
def create(request):
	if request.method == 'POST':
		postform = bforms.PostForm(request.POST)
		if postform.is_valid():
			post = postform.save()
			return HttpResponseRedirect('/')
	else:
		postform = bforms.PostForm()
	payload = dict(postform = postform)
	return render('create.html', payload)
#	return render_to_response('create.html',{'postform':postform})

def comment(request, post_key):
	post = models.Post.get(post_key)
	if request.method == 'POST':
		commentform = bforms.CommentForm(request.POST)
		if commentform.is_valid():
			comment = commentform.save()
			return HttpResponseRedirect('/')
	else:
		commentform = bforms.CommentForm()
#	payload = dict(post , post)
#	return render('post.html', payload)
	return render_to_response('comment.html',{'post':post,'commentform':commentform})


